package com.hospital.controller;

import com.hospital.dao.impl.PatientDao;
import com.hospital.model.DoctorView;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.SQLException;
/**
 * 处理患者出院请求，更新患者状态并返回操作结果
 */
@WebServlet("/discharge")
public class DischargeServlet extends HttpServlet {
    /** 患者数据访问对象，用于处理患者出院相关的数据库操作 */
    private PatientDao patientDao = new PatientDao();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        
        resp.setContentType("application/json;charset=UTF-8");
        
        // 获取当前登录的医生信息
        HttpSession session = req.getSession();
        DoctorView doctor = (DoctorView) session.getAttribute("doctor");
        
        if (doctor == null) {
            resp.setStatus(401);
            resp.getWriter().write("{\"error\": \"未登录\"}");
            return;
        }

        try {
            // 获取病人ID
            String patientIdStr = req.getParameter("patientId");
            if (patientIdStr == null || patientIdStr.trim().isEmpty()) {
                resp.setStatus(400);
                resp.getWriter().write("{\"error\": \"病人ID不能为空\"}");
                return;
            }

            int patientId;
            try {
                patientId = Integer.parseInt(patientIdStr);
            } catch (NumberFormatException e) {
                resp.setStatus(400);
                resp.getWriter().write("{\"error\": \"无效的病人ID\"}");
                return;
            }

            // 执行出院操作
            boolean success = patientDao.dischargePatient(patientId);
            
            if (success) {
                resp.getWriter().write("{\"message\": \"出院办理成功\"}");
            } else {
                resp.setStatus(500);
                resp.getWriter().write("{\"error\": \"出院办理失败\"}");
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
            resp.setStatus(500);
            if (e.getMessage().contains("foreign key")) {
                resp.getWriter().write("{\"error\": \"数据库错误：找不到相关的病人记录\"}");
            } else {
                resp.getWriter().write("{\"error\": \"数据库错误：" + e.getMessage() + "\"}");
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            resp.setStatus(400);
            resp.getWriter().write("{\"error\": \"参数错误：" + e.getMessage() + "\"}");
        } catch (SecurityException e) {
            e.printStackTrace();
            resp.setStatus(403);
            resp.getWriter().write("{\"error\": \"权限错误：" + e.getMessage() + "\"}");
        } catch (IOException e) {
            e.printStackTrace();
            resp.setStatus(500);
            resp.getWriter().write("{\"error\": \"IO错误：" + e.getMessage() + "\"}");
        }
    }
} 